package com.rfid.afd.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.TextView;

import com.rfid.afd.activity.adapter.ShangjiaAdapter;
import com.rfid.afd.activity.adapter.StatisticsAdapter;
import com.rfid.afd.activity.view.IShangjiaView;
import com.rfid.afd.presenter.ShangjiaPresenter;
import com.rfid.db.bean.Rfid;
import com.rfid.rls_psw.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
/**上架，单纯的扫描标签得到书籍信息，方便工作人员获知书籍该放在哪个位置*/
public class ShangjiaActivity extends BaseActivity<ShangjiaPresenter> implements IShangjiaView {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.tip)
    TextView mTip;
    @BindView(R.id.quit)
    Button mQuit;
    ShangjiaAdapter myAdapter;
    private List<Rfid> mRfids;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shangjia);
        ButterKnife.bind(this);
        mRfids = new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        myAdapter = new ShangjiaAdapter(this,mRfids);
        mRecyclerView.setAdapter(myAdapter);
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mRecyclerView.setLayoutAnimation(controller);
    }

    @Override
    public ShangjiaPresenter getPresenter() {
        return new ShangjiaPresenter();
    }

    @Override
    public void onRfidScaned(Rfid rfid) {
        if(!mRfids.contains(rfid)) {
            mRfids.add(rfid);
            mPresenter.addShangjiaLog(rfid);
            myAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public void onBackPressed() {
        if(isSearch) {
            return;
        }
        super.onBackPressed();
        mPresenter.stopScanRfid();
        mPresenter.updateOperation();
    }

    @Override
    public void finish() {
        super.finish();
        mPresenter.stopScanRfid();
        mPresenter.updateOperation();
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void stopLoading() {
    }
    boolean isSearch = false;
    public void search(View view) {
        TextView textView = (TextView) view;
        if(!isSearch) {
            textView.setText("停止");
            mQuit.setEnabled(false);
            mPresenter.startScanRfid();
        } else {
            textView.setText("扫描");
            mQuit.setEnabled(true);
            mPresenter.stopScanRfid();
        }
        isSearch = !isSearch;
    }

    public void quit(View view) {
        onBackPressed();
    }
}